﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using AnalisisDeRiesgos.Dal;

namespace AnalisisDeRiesgos.Models.Process
{
    public class EditProcessModel
    {
        public int ProcessId { get; set; }

        [DisplayName("Nombre del Proceso")]
        [Required(ErrorMessage = "El nombre del proceso es requerido")]
        [StringLength(50, ErrorMessage = "El nombre debe ser máximo de 50 caracteres")]
        [RegularExpression(@"^[a-zA-Z0-9 \(\)ñÑ\-.,áéíóúÁÉÍÓÚ]+$", ErrorMessage = "El nombre debe ser solo letras o números")]
        public string Name { get; set; }

        [DisplayName("Jefatura/Gerencia")]
        [Required(ErrorMessage = "Una jefatura/gerencia es necesaria")]
        public int Management { get; set; }

        public IEnumerable<SelectListItem> Managements
        {
            get
            {
                return managementList;
            }
        }

        private List<SelectListItem> managementList;
        riskanalysisEntities entities = new riskanalysisEntities();

        public EditProcessModel()
        {
            FillList();
        }

        public EditProcessModel(int processId)
            : this()
        {
            FillList();
            FillProcessById(processId);
        }

        private void FillList()
        {
            managementList = new List<SelectListItem>();
            
            var queryManagements = from m in entities.managements
                                   where m.enabled
                                   select m;

            foreach (management item in queryManagements)
            {
                managementList.Add(
                    new SelectListItem
                    {
                        Text = item.name,
                        Value = item.managementId.ToString()
                    });
            }
        }

        private void FillProcessById(int id)
        {
            process theProcess = GetProcessById(id);

            ProcessId = theProcess.processId;
            Name = theProcess.name;
            Management = theProcess.managementId;
        }

        private process GetProcessById(int id)
        {
            var query = from p in entities.processes
                        where p.processId == id && p.enabled
                        select p;

            process theProcess = query.FirstOrDefault();

            if (theProcess == null)
                throw new EditProcessException("El proceso que usted busca no existe");
            return theProcess;
        }

        public void Save()
        {
            process theManagement = GetProcessById(ProcessId);

            theManagement.name = Name;
            theManagement.managementId = Management;           
            entities.SaveChanges();           
        }
    }
}